from fastapi import FastAPI
import uvicorn

import os
import sys
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
if project_root not in sys.path:
    sys.path.append(project_root)

from readlater.web.routes import router as api_router
from readlater.database import DatabaseManager
from readlater.config import ConfigManager



# 创建FastAPI应用
app = FastAPI(
    title="Chatlog API",
    description="聊天记录查询API服务",
    version="1.0.0"
)

# 包含API路由
app.include_router(api_router)

# 启动服务
if __name__ == "__main__":
    # 初始化数据库
    config_manager = ConfigManager()
    if config_manager.is_first_run():
        db_manager = DatabaseManager(config_manager.get_database_path())
        db_manager.connect()
        db_manager.create_table()
        db_manager.close()
    uvicorn.run(app, host=config_manager.get('web.host'), port=int(config_manager.get('web.port')))
